package com.hzf.wxplatform.web.controller.manager.goods;

import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.material.dao.model.*;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/11/27   14:51
 **/
@Controller
@RequestMapping("/materialGoods")
public class MaterialGoodsController extends BaseController {
    @Autowired
    ApiService apiService;

    @RequestMapping(value = "/index")
    private String index(Model model, HttpServletRequest request,@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                         @RequestParam(required = false, defaultValue = "3", value = "limit") int limit,
                         @RequestParam(required = false, value = "sort") String sort,
                         @RequestParam(required = false, value = "order") String order){
        byte [] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);     if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andShopIdEqualTo(materialShop.getShopId()).andUseIdEqualTo(ucenterUser.getId()).andStatusEqualTo(0);
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materialGoodsExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialGoods> list = apiService.selectGoodsByExampleForOffsetPage(materialGoodsExample,offset,limit);
        long total = apiService.countGoodsByExample(materialGoodsExample);

        System.out.println(list.size());
        List<MaterialGoodsModel> models= new ArrayList<>();
        for(MaterialGoods materialGoods:list){
            MaterialGoodsModelExample materialGoodsModelExample  = new MaterialGoodsModelExample();
            materialGoodsModelExample.createCriteria().andGoodsIdEqualTo(materialGoods.getGoodsId());
            models = apiService.selectByMaterialGoodsModel(materialGoodsModelExample);
            Collections.sort(models);
            if(0!=models.size()){
                MaterialGoodsModel materialGoodsModel = new MaterialGoodsModel();
                System.out.println(materialGoodsModel.getPrice());
                materialGoods.setModel(models.get(0));
            }else {
                MaterialGoodsModel materialGoodsModel = new MaterialGoodsModel();
                materialGoodsModel.setPrice(0.00);
                materialGoodsModel.setGoodsId(materialGoods.getGoodsId());
                materialGoodsModel.setImgUrl("1221");
                materialGoods.setModel(materialGoodsModel);
            }

        }
        long tiao=limit-offset;
        System.out.println(tiao);
        System.out.println(total);
        double yeshu=(double)total/tiao;
        int ye = (int)Math.ceil(yeshu);
        model.addAttribute("ye",ye);
        model.addAttribute("total",total);
        model.addAttribute("limit",limit);
        model.addAttribute("offset",offset);
        model.addAttribute("result",list);
        return thymeleaf("/CLS/CLS_SG_AddShop");
    }
    @RequestMapping(value = "/nextPage",method = RequestMethod.POST)
    @ResponseBody
    private Object nextPage(Model model, HttpServletRequest request,@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                         @RequestParam(required = false, defaultValue = "3", value = "limit") int limit,
                         @RequestParam(required = false, value = "sort") String sort,
                         @RequestParam(required = false, value = "order") String order){
        System.out.println(1111);
        byte [] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);  if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andShopIdEqualTo(materialShop.getShopId()).andUseIdEqualTo(ucenterUser.getId()).andStatusEqualTo(0);
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materialGoodsExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialGoods> list = apiService.selectGoodsByExampleForOffsetPage(materialGoodsExample,offset,limit);
        long total = apiService.countGoodsByExample(materialGoodsExample);

        System.out.println(list.size());
        List<MaterialGoodsModel> models= new ArrayList<>();
        for(MaterialGoods materialGoods:list){
            MaterialGoodsModelExample materialGoodsModelExample  = new MaterialGoodsModelExample();
            materialGoodsModelExample.createCriteria().andGoodsIdEqualTo(materialGoods.getGoodsId());
            models = apiService.selectByMaterialGoodsModel(materialGoodsModelExample);
            Collections.sort(models);
            if(0!=models.size()){
                MaterialGoodsModel materialGoodsModel = new MaterialGoodsModel();
                materialGoods.setModel(models.get(0));
            }else {
                MaterialGoodsModel materialGoodsModel = new MaterialGoodsModel();
                materialGoodsModel.setPrice(0.00);
                materialGoodsModel.setGoodsId(materialGoods.getGoodsId());
                materialGoodsModel.setImgUrl("1221");
                materialGoods.setModel(materialGoodsModel);
            }

        }

        return new WxplatformResult(WxplatformResultConstant.SUCCESS, list);
    }
    @RequestMapping(value = "/setPindan")
    public String setPindan(Model model,HttpServletRequest servletRequest,int goodsId){
        List<MaterialGoodsModel> models= new ArrayList<>();
        MaterialGoodsModelExample materialGoodsModelExample  = new MaterialGoodsModelExample();
        materialGoodsModelExample.createCriteria().andGoodsIdEqualTo(goodsId);
        models = apiService.selectByMaterialGoodsModel(materialGoodsModelExample);
        model.addAttribute("result",models);
        return thymeleaf("/CLS/CLS_SG_PindanAdd");
    }
    @RequestMapping(value = "/addGoods")
    public String addGoods(Model model,HttpServletRequest request){
        MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
        List<MaterialGoodsTypeBig> bigs = apiService.selectByMaterialGoodsTypeBig(materialGoodsTypeBigExample);
        for(MaterialGoodsTypeBig materialGoodsTypeBig :bigs) {
            MaterialGoodsTypeMiddleExample materialGoodsTypeMiddleExample = new MaterialGoodsTypeMiddleExample();
            materialGoodsTypeMiddleExample.createCriteria().andBigIdEqualTo(materialGoodsTypeBig.getBigId());
            List<MaterialGoodsTypeMiddle> typeMiddles = apiService.selectByMaterialGoodsTypeMiddle(materialGoodsTypeMiddleExample);

                for (MaterialGoodsTypeMiddle materialGoodsTypeMiddle : typeMiddles) {
                    MaterialGoodsTypeSmallExample materialGoodsTypeSmallExample = new MaterialGoodsTypeSmallExample();
                    materialGoodsTypeSmallExample.createCriteria().andMiddleIdEqualTo(materialGoodsTypeMiddle.getMiddleId());
                    List<MaterialGoodsTypeSmall> typeSmalls = apiService.selectByMaterialGoodsTypeSmall(materialGoodsTypeSmallExample);
                    if(typeSmalls.size()==0){
                        MaterialGoodsTypeSmall materialGoodsTypeSmall = new MaterialGoodsTypeSmall();
                        materialGoodsTypeSmall.setSmallName("其他");
                        typeSmalls.add(materialGoodsTypeSmall);
                    }
                    materialGoodsTypeMiddle.setMaterialGoodsTypeSmallList(typeSmalls);
                }
                materialGoodsTypeBig.setMaterialGoodsTypeMiddleList(typeMiddles);

        }
        model.addAttribute("bigs",bigs);
        return thymeleaf("/CLS/CLS_SG_AddCaShop");

    }
    @RequestMapping(value = "/goodsSave",method = RequestMethod.POST)
    @ResponseBody
    private Object goodsSave(Model model,MaterialGoods materialGoods,HttpServletRequest request) throws Exception{
        byte [] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);  if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        System.out.println(materialGoods.getModelList().size());
        materialGoods.setShopId(materialShop.getShopId());
        materialGoods.setUseId(ucenterUser.getId());
        materialGoods.setStatus(0);
        materialGoods.setSales(0);
        materialGoods.setLng(materialShop.getLng());
        materialGoods.setLat(materialShop.getLat());
        materialGoods.setGeoCode(materialShop.getGeoCode());
        materialGoods = apiService.installGoods(materialGoods);
        for(MaterialGoodsModel materialGoodsModel : materialGoods.getModelList()){
            materialGoodsModel.setGoodsId(materialGoods.getGoodsId());
            apiService.installGoodsModel(materialGoodsModel);
        }
        System.out.println(materialGoods.getGoodsId());
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, materialGoods.getGoodsId());


    }
    @RequestMapping(value = "/pindanSave",method = RequestMethod.POST)
    @ResponseBody
    private Object pindanSave(Model model,MaterialGoodsModelPin materialGoodsModelPin,HttpServletRequest request) throws Exception{
        for(MaterialPicInfo materialPicInfo : materialGoodsModelPin.getList()){
            materialGoodsModelPin.setModelId(materialPicInfo.getModelId());
            materialGoodsModelPin.setPinPrice(materialPicInfo.getPinPrice());
            apiService.installGoodsPin(materialGoodsModelPin);
        }
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");
    }
    @RequestMapping(value="/shopXq")
    private String shopXq(Model model,HttpServletRequest request,int goodsId)throws  Exception{
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MaterialGoods materialGoods = apiService.selectByGoods(materialGoodsExample);
        MaterialGoodsModelExample example = new MaterialGoodsModelExample();
        example.createCriteria().andGoodsIdEqualTo(materialGoods.getGoodsId());
        MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
        MaterialGoodsTypeMiddleExample materialGoodsTypeMiddleExample = new MaterialGoodsTypeMiddleExample();
        MaterialGoodsTypeSmallExample materialGoodsTypeSmallExample = new MaterialGoodsTypeSmallExample();
        materialGoodsTypeBigExample.createCriteria().andBigIdEqualTo(materialGoods.getTypeBigId());
        materialGoodsTypeMiddleExample.createCriteria().andMiddleIdEqualTo(materialGoods.getTypeMiddleId());
        materialGoodsTypeSmallExample.createCriteria().andSmallIdEqualTo(materialGoods.getTypeSmallId());
        materialGoods.setMaterialGoodsTypeBig(apiService.selectBig(materialGoodsTypeBigExample));
        materialGoods.setMaterialGoodsTypeMiddle(apiService.selectMiddle(materialGoodsTypeMiddleExample));
        materialGoods.setMaterialGoodsTypeSmall(apiService.selectSmall(materialGoodsTypeSmallExample));
        List<MaterialGoodsModel> models= apiService.selectByMaterialGoodsModel(example);

        materialGoods.setModelList(models);
        List<MaterialGoodsModelPin> list = new ArrayList<>();
        List<MaterialGoodsModel> modellist1 = new ArrayList<>();

        if(0!=models.size()){
            if(materialGoods.getIsOpenpin()==1){
                for(MaterialGoodsModel materialGoodsModel:models){
                    MaterialGoodsModelPinExample materialGoodsModelPinExample = new MaterialGoodsModelPinExample();
                    materialGoodsModelPinExample.createCriteria().andModelIdEqualTo(materialGoodsModel.getModelId());
                    MaterialGoodsModelPin materialGoodsModelPin = apiService.selectModelPin(materialGoodsModelPinExample);
                    if(null!=materialGoodsModelPin){
                        list.add(materialGoodsModelPin);
                        materialGoodsModel.setPinPrice(materialGoodsModelPin.getPinPrice());
                        modellist1.add(materialGoodsModel);
                    }

                }
                if(0!=list.size()) {
                    materialGoods.setMaterialGoodsModelPin(list.get(0));
                    materialGoods.setPinmodelList(modellist1);
                }
                model.addAttribute("data",materialGoods);
                return thymeleaf("/CLS/CLS_SGShopXq");


            }
            else{
                model.addAttribute("data",materialGoods);
                return thymeleaf("/CLS/CLS_SGShopXq");
            }
        }else {
            model.addAttribute("data",materialGoods);
            return thymeleaf("/CLS/CLS_SGShopXq");


        }

    }
    @RequestMapping(value="/updateGoods")
    private String updateGoods(Model model,HttpServletRequest request,int goodsId)throws  Exception{
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MaterialGoods materialGoods = apiService.selectByGoods(materialGoodsExample);
        MaterialGoodsModelExample example = new MaterialGoodsModelExample();
        example.createCriteria().andGoodsIdEqualTo(materialGoods.getGoodsId());
        List<MaterialGoodsModel> models= apiService.selectByMaterialGoodsModel(example);
        if(null!=models){
            MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
            MaterialGoodsTypeMiddleExample materialGoodsTypeMiddleExample = new MaterialGoodsTypeMiddleExample();
            MaterialGoodsTypeSmallExample materialGoodsTypeSmallExample = new MaterialGoodsTypeSmallExample();
            materialGoodsTypeBigExample.createCriteria().andBigIdEqualTo(materialGoods.getTypeBigId());
            materialGoodsTypeMiddleExample.createCriteria().andMiddleIdEqualTo(materialGoods.getTypeMiddleId());
            materialGoodsTypeSmallExample.createCriteria().andSmallIdEqualTo(materialGoods.getTypeSmallId());
            materialGoods.setMaterialGoodsTypeBig(apiService.selectBig(materialGoodsTypeBigExample));
            materialGoods.setMaterialGoodsTypeMiddle(apiService.selectMiddle(materialGoodsTypeMiddleExample));
            materialGoods.setMaterialGoodsTypeSmall(apiService.selectSmall(materialGoodsTypeSmallExample));
        }
        MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
        List<MaterialGoodsTypeBig> bigs = apiService.selectByMaterialGoodsTypeBig(materialGoodsTypeBigExample);
        for(MaterialGoodsTypeBig materialGoodsTypeBig :bigs) {
            MaterialGoodsTypeMiddleExample materialGoodsTypeMiddleExample = new MaterialGoodsTypeMiddleExample();
            materialGoodsTypeMiddleExample.createCriteria().andBigIdEqualTo(materialGoodsTypeBig.getBigId());
            List<MaterialGoodsTypeMiddle> typeMiddles = apiService.selectByMaterialGoodsTypeMiddle(materialGoodsTypeMiddleExample);

            for (MaterialGoodsTypeMiddle materialGoodsTypeMiddle : typeMiddles) {
                MaterialGoodsTypeSmallExample materialGoodsTypeSmallExample = new MaterialGoodsTypeSmallExample();
                materialGoodsTypeSmallExample.createCriteria().andMiddleIdEqualTo(materialGoodsTypeMiddle.getMiddleId());
                List<MaterialGoodsTypeSmall> typeSmalls = apiService.selectByMaterialGoodsTypeSmall(materialGoodsTypeSmallExample);
                if(typeSmalls.size()==0){
                    MaterialGoodsTypeSmall materialGoodsTypeSmall = new MaterialGoodsTypeSmall();
                    materialGoodsTypeSmall.setSmallName("其他");
                    typeSmalls.add(materialGoodsTypeSmall);
                }
                materialGoodsTypeMiddle.setMaterialGoodsTypeSmallList(typeSmalls);
            }
            materialGoodsTypeBig.setMaterialGoodsTypeMiddleList(typeMiddles);

        }
        model.addAttribute("bigs",bigs);
        materialGoods.setModelList(models);
        List<MaterialGoodsModelPin> list = new ArrayList<>();
        List<MaterialGoodsModel> modellist1 = new ArrayList<>();

        if(0!=models.size()){
            if(materialGoods.getIsOpenpin()==1){
                for(MaterialGoodsModel materialGoodsModel:models){
                    MaterialGoodsModelPinExample materialGoodsModelPinExample = new MaterialGoodsModelPinExample();
                    materialGoodsModelPinExample.createCriteria().andModelIdEqualTo(materialGoodsModel.getModelId());
                    MaterialGoodsModelPin materialGoodsModelPin = apiService.selectModelPin(materialGoodsModelPinExample);
                    if(null!=materialGoodsModelPin){
                        list.add(materialGoodsModelPin);
                        materialGoodsModel.setPinPrice(materialGoodsModelPin.getPinPrice());
                        modellist1.add(materialGoodsModel);
                    }

                }
                if(0!=list.size()) {
                    materialGoods.setMaterialGoodsModelPin(list.get(0));
                    materialGoods.setPinmodelList(modellist1);
                }
                model.addAttribute("xinghao",materialGoods.getModelList().size());
                model.addAttribute("data",materialGoods);
                return thymeleaf("/CLS/CLS_SG_ChangeShop");


            }
            else{
                model.addAttribute("xinghao",materialGoods.getModelList().size());
                model.addAttribute("data",materialGoods);
                return thymeleaf("/CLS/CLS_SG_ChangeShop");
            }
        }else {
            model.addAttribute("xinghao",materialGoods.getModelList().size());
            model.addAttribute("data",materialGoods);
            return thymeleaf("/CLS/CLS_SG_ChangeShop");


        }

    }
    @RequestMapping(value = "/goodsUpdate",method = RequestMethod.POST)
    @ResponseBody
    private Object goodsUpdate(Model model,MaterialGoods materialGoods,HttpServletRequest request) throws Exception{
        byte [] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null!=ucenterUser){
            if(ucenterUser.getId().equals(materialGoods.getUseId())){
                int count = apiService.updateGoods(materialGoods);
                if(count==1){
                    if(null!=materialGoods.getDilectModel()){
                    for(MaterialGoodsModel materialGoodsModel1:materialGoods.getDilectModel()){
                        MaterialGoodsModelExample materialGoodsModelExample = new MaterialGoodsModelExample();
                        materialGoodsModelExample.createCriteria().andModelIdEqualTo(materialGoodsModel1.getModelId());
                        MaterialGoodsModelPinExample materialGoodsModelPinExample = new MaterialGoodsModelPinExample();
                        materialGoodsModelPinExample.createCriteria().andModelIdEqualTo(materialGoodsModel1.getModelId());
                        MaterialGoodsModelPin materialGoodsModelPin=apiService.selectModelPin(materialGoodsModelPinExample);
                        if(null!=materialGoodsModelPin){
                            return new WxplatformResult(WxplatformResultConstant.FAILED, "不可删除型号，型号还存在拼单");

                        }
                        apiService.delectModels(materialGoodsModelExample);

                    }}
                    for(MaterialGoodsModel materialGoodsModel : materialGoods.getModelList()){
                        if(null!=materialGoodsModel.getModelId()){
                            materialGoodsModel.setGoodsId(materialGoods.getGoodsId());
                            apiService.updateGoodsModel(materialGoodsModel);
                        }else{
                            if(materialGoodsModel.getColor()==null||materialGoodsModel.getPrice()==0){

                            }else{
                                materialGoodsModel.setGoodsId(materialGoods.getGoodsId());
                                apiService.installGoodsModel(materialGoodsModel);}

                        }

                    }
                }

                return new WxplatformResult(WxplatformResultConstant.SUCCESS, materialGoods.getGoodsId());

            }else{
                return new WxplatformResult(WxplatformResultConstant.FAILED, "非法操作，当前用户非店铺管理");
            }
        }else{
            return new WxplatformResult(WxplatformResultConstant.FAILED, "非法操作，未登录");

        }
    }
    @RequestMapping(value = "/xiajiaGoods",method = RequestMethod.POST)
    @ResponseBody
    public Object xiajiaGoods(Model model,HttpServletRequest request,int goodsId) throws Exception{
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MaterialGoods materialGoods = apiService.selectByGoods(materialGoodsExample);
        if(materialGoods.getStatus()==0){
            materialGoods.setStatus(1);
        }else{
            materialGoods.setStatus(0);

        }
        int count=apiService.updateGoods(materialGoods);
        if(count>0){
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "下架成功");
        }else{
            return new WxplatformResult(WxplatformResultConstant.FAILED, "下架失败，请重试");

        }

    }
    @RequestMapping(value = "/shanchuGoods",method = RequestMethod.POST)
    @ResponseBody
    public Object shanchuGoods(Model model,HttpServletRequest request,int goodsId) throws Exception{
        byte [] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MaterialGoods materialGoods = apiService.selectByGoods(materialGoodsExample);
        if(ucenterUser.getId().equals(materialGoods.getUseId())){
            if(materialGoods.getIsOpenpin()==1){
                return new WxplatformResult(WxplatformResultConstant.FAILED, "已设置拼单，请取消拼单后删除");
            }
            int count=apiService.delectGoods(goodsId);
            if(count>0){
                MaterialGoodsModelExample materialGoodsModelExample = new MaterialGoodsModelExample();
                materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
                int modelCount = apiService.delectModels(materialGoodsModelExample);
                if(modelCount>0){
                    return new WxplatformResult(WxplatformResultConstant.SUCCESS, "删除商品成功");
                }else{
                    return new WxplatformResult(WxplatformResultConstant.SUCCESS, "删除商品成功");

                }
            }else{
                return new WxplatformResult(WxplatformResultConstant.FAILED, "删除失败，请重试");

            }
        }else {
            return new WxplatformResult(WxplatformResultConstant.FAILED, "非法操作，登陆用户非店铺管理者");

        }



    }
    @RequestMapping(value="/updateGoodsPin")
    private String updateGoodsPin(Model model,HttpServletRequest request,int goodsId)throws  Exception{
        MaterialGoodsExample materialGoodsExample = new MaterialGoodsExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MaterialGoods materialGoods = apiService.selectByGoods(materialGoodsExample);
        List<MaterialGoodsModel> list = new ArrayList<>();
        MaterialGoodsModelExample materialGoodsModelExample = new MaterialGoodsModelExample();
        materialGoodsExample.createCriteria().andGoodsIdEqualTo(goodsId);
        list = apiService.selectByMaterialGoodsModel(materialGoodsModelExample);
        List<MaterialGoodsModelPin> pinList = new ArrayList<>();
        List<MaterialGoodsModel> modellist1 = new ArrayList<>();
        if(0!=list.size()) {
            materialGoods.setModelList(list);

            if (materialGoods.getIsOpenpin() == 1) {
                for (MaterialGoodsModel materialGoodsModel : list) {
                    MaterialGoodsModelPinExample materialGoodsModelPinExample = new MaterialGoodsModelPinExample();
                    materialGoodsModelPinExample.createCriteria().andModelIdEqualTo(materialGoodsModel.getModelId());
                    MaterialGoodsModelPin materialGoodsModelPin = new MaterialGoodsModelPin();
                    materialGoodsModelPin = apiService.selectModelPin(materialGoodsModelPinExample);
                    if (null != materialGoodsModelPin) {
                        pinList.add(materialGoodsModelPin);
                        materialGoodsModel.setPinPrice(materialGoodsModelPin.getPinPrice());
                        modellist1.add(materialGoodsModel);
                    }
                }
                if(0==pinList.size()){
                    model.addAttribute("result",list);
                    return thymeleaf("/CLS/CLS_SG_PindanAdd");
                }else{
                    if (0 != list.size()) {
                        if(0!=pinList.size()){
                            materialGoods.setMaterialGoodsModelPin(pinList.get(0));
                            materialGoods.setPinmodelList(modellist1);
                            model.addAttribute("xinghao", materialGoods.getPinmodelList().size());

                        }else{
                            model.addAttribute("xinghao", 0);

                        }

                    }
                }

                model.addAttribute("data", materialGoods);
                return thymeleaf("/CLS/CLS_SG_PindanChange");

            } else {
                model.addAttribute("result",list);
                return thymeleaf("/CLS/CLS_SG_PindanAdd");
            }
        }
        model.addAttribute("xinghao", 0);
        model.addAttribute("data", materialGoods);
        return thymeleaf("/CLS/CLS_SG_PindanChange");
    }

    @RequestMapping(value = "/pindanUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object pindanUpdate(Model model,HttpServletRequest request,MaterialGoodsModelPin materialGoodsModelPin) throws Exception {
        if(0!=materialGoodsModelPin.getDelectlist().size()){
            for(MaterialPicInfo materialPicInfo:materialGoodsModelPin.getDelectlist()){
                MaterialGoodsModelPinExample materialGoodsModelPin1Example = new MaterialGoodsModelPinExample();
                materialGoodsModelPin1Example.createCriteria().andModelIdEqualTo(materialPicInfo.getModelId());
                apiService.delectGoodsModelPin(materialGoodsModelPin1Example);
            }
        }
        if(0!=materialGoodsModelPin.getList().size()) {
            for (MaterialPicInfo materialPicInfo : materialGoodsModelPin.getList()) {

                MaterialGoodsModelPinExample materialGoodsModelPinExample = new MaterialGoodsModelPinExample();
                materialGoodsModelPinExample.createCriteria().andModelIdEqualTo(materialPicInfo.getModelId());
                MaterialGoodsModelPin materialGoodsModelPin1 = apiService.selectModelPin(materialGoodsModelPinExample);
                if (null != materialGoodsModelPin1) {
                    materialGoodsModelPin.setPinId(materialGoodsModelPin1.getPinId());
                    apiService.updateGoodsModelPin(materialGoodsModelPin);
                } else {
                    apiService.installGoodsPin(materialGoodsModelPin);
                }
            }
        }else{
            return new WxplatformResult(WxplatformResultConstant.FAILED, "请设置拼单型号");

        }
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");

    }
}
